package com.example.baiduai.control;
import com.example.baiduai.dao.impl.GenericDaoImpl;
import com.example.baiduai.entity.JsonGenericBean;
import com.example.baiduai.service.SpeechService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc: 语言技术引擎模块的控制器
 * @Time 2022/09/20 11:35
 */

public class SpeechControl implements Controller {
    public void  handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String per = req.getParameter("per");
        String type =req.getParameter("type");
        PrintWriter writer=resp.getWriter();
        SpeechService speechService=new SpeechService();
        GenericDaoImpl speechDao=new GenericDaoImpl();
        switch (type){
            case "audio_merge":
                String content=req.getParameter("content");
                String str=content.replaceAll(" ","");
                String spd=req.getParameter("spd");
                String pit=req.getParameter("pit");
                String vol=req.getParameter("vol");
                JsonGenericBean speechBean = speechDao.select("语音合成",str);
                if (speechBean==null){
                    System.out.println("数据库无数据");
                    String s = speechService.audio_merge(str, spd, pit, vol, per);
                    int i=speechDao.insert("语音合成",str,s);
                    if (i==1){
                        System.out.println("添加成功");
                    }
                    writer.write(s);
                }else {
                    System.out.println("数据库有数据");
                    writer.write(speechBean.getJson());
                }
                break;
            default:
                break;
        }
        writer.flush();
        writer.close();

    }

}

